<?php 
/*
 * module post
 * 
 */
 
 class posts{
 	
	protected $_db;
    protected $err_msg = '';
	function __construct(){
		$this->_db = db::getInstance();
		$this->v = new view();
		$this->v->init(TRUE);
	}
	/****************************************controller ********************************************************/
	/*
	 * list posts
	 */ 
	public function index()
	{
		$page = isset($_REQUEST['page'])?$_REQUEST['page']:'0';
		$list = $this->post_list($page);
		$data = array();
		$data['list'] = $list;		
		$this->v->load('header');
		$this->v->load('post_list', $data);
		$this->v->load('footer');
		
	}
	
	/*
	 * add post
	 * edit post
	 */
	public function edit()
	{
        $p = array();
        if(isset($_GET['id'])){
            $id = intval($_GET['id']);
            $p = $this->post_info($id);
        }
        $cat_list = get_category_list();
        $cat_list = cat_options(0, $cat_list);

        $data = array();
        $data['p'] = $p;
        $data['cat_list'] = $cat_list;
        $this->v->load('header');
        $this->v->load('post_edit', $data);
        $this->v->load('footer');
	}

     /*
      *
      */
     public function save(){
         $post = $_POST;
         if(empty($post)){
             $this->err_msg = '缺少参数';
         }
         $data = array();
         if(empty($post['title'])){
             $this->err_msg .= ' title can not empty ';
         }
         $data['title'] = $post['title'];
         if(empty($post['category'])){
             $this->err_msg .= ' category can not empty ';
         }
         $data['category'] = $post['category'];
         if(empty($post['content'])){
             $this->err_msg .= 'content can not empty';
         }
         $data['content'] = $post['content'];
         $data['source'] = !empty($post['source'])?$post['source']:'本站';
         $data['author'] = '0';
         if(empty($post['id'])){
             $data['click'] = 0;
         }
         $data['status'] = 0;
         $data['post_date'] = date('Y-m-d H:i:s');
         if($this->err_msg != ''){
             $this->error();
             return false;
         }
         if(empty($post['id'])){

             return $this->_db->insert('posts', $data);
         }else{
             if(!is_array($data)){
                 return FALSE;
             }
             $sql = "UPDATE posts set ";
             foreach($data as $key=>$val){
                 $sql .= $key."='".$val."' ,";
             }
             $sql = rtrim($sql,',');
             $sql .=" where id='{$post['id']}'";
             $this->_db->query($sql);
         }
         redirect('spider/spider.php?mod=posts');
     }

	/*
	 * del post
	 */ 
	public function del()
	{
		
	}

     /*
      *
      */
    public function error(){
        echo $this->err_msg;
    }
	    
	/*********************************     model ******************************/

	
	protected function post_info($id){
		$sql = "SELECT * FROM posts WHERE id='$id'";
		$post = $this->_db->query($sql)->result_array();
		if($post == FALSE){
			return FALSE;
		}
		return $post[0];
	}
	
	protected function post_list($page, $size=15){
		$sql = "SELECT p.*,c.cat_name FROM posts as p JOIN category as c ON p.category=c.cid WHERE p.status=0";
		$sql .= " ORDER BY p.id DESC LIMIT ".$page*$size.",".$size;	
		$list = $this->_db->query($sql)->result_array();
		return $list;
	}
 }
